<template>
  <div class="ad-banner">
    <div
      class="banner-main"
      @mouseenter="stopRotation"
      @mouseleave="startRotation"
    >
      <div class="carousel-container">
        <div
          class="carousel-item"
          v-for="(image, index) in carouselImages"
          :key="index"
          :class="{ active: currentIndex === index }"
        >
          <img :src="image.url" :alt="image.alt" />
        </div>
        <button class="carousel-control prev" @click="prevSlide">
          &#10094;
        </button>
        <button class="carousel-control next" @click="nextSlide">
          &#10095;
        </button>
        <div class="carousel-indicators">
          <span
            v-for="(image, index) in carouselImages"
            :key="index"
            :class="{ active: currentIndex === index }"
            @click="goToSlide(index)"
          ></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "AdBanner",
  data() {
    return {
      carouselImages: [
        {
          url: "/lunbo/shopping-1.jpg",
          alt: "Pet Food Discount",
        },
        {
          url: "/lunbo/shopping-2.jpg",
          alt: "New Pet Toys",
        },
        {
          url: "/lunbo/shopping-3.jpg",
          alt: "Pet Grooming Sale",
        },
      ],
      currentIndex: 0,
      rotationInterval: null,
      rotationSpeed: 3000, // 3秒轮播一次
    };
  },
  methods: {
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.carouselImages.length;
    },
    prevSlide() {
      this.currentIndex =
        (this.currentIndex - 1 + this.carouselImages.length) %
        this.carouselImages.length;
    },
    goToSlide(index) {
      this.currentIndex = index;
    },
    startRotation() {
      this.stopRotation(); // 先停止现有的定时器
      this.rotationInterval = setInterval(this.nextSlide, this.rotationSpeed);
    },
    stopRotation() {
      if (this.rotationInterval) {
        clearInterval(this.rotationInterval);
        this.rotationInterval = null;
      }
    },
  },
  mounted() {
    this.startRotation();
  },
  beforeDestroy() {
    this.stopRotation();
  },
};
</script>

<style scoped>
.ad-banner {
  display: flex;
  margin-bottom: 20px;
  height: 300px;
  width: 70%;
  padding-right: 10px;
}

.banner-main {
  flex: 3;
  margin-right: 10px;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
}

.carousel-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.carousel-item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.5s ease-in-out;
}

.carousel-item.active {
  opacity: 1;
}

.carousel-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-caption {
  position: absolute;
  bottom: 20px;
  left: 20px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  max-width: 60%;
}

.carousel-caption h3 {
  margin: 0 0 5px 0;
  font-size: 24px;
}

.carousel-caption p {
  margin: 0;
  font-size: 14px;
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.3);
  color: white;
  border: none;
  padding: 10px;
  cursor: pointer;
  font-size: 18px;
  z-index: 10;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.carousel-control:hover {
  background-color: rgba(0, 0, 0, 0.6);
}

.carousel-control.prev {
  left: 15px;
}

.carousel-control.next {
  right: 15px;
}

.carousel-indicators {
  position: absolute;
  bottom: 15px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 10;
}

.carousel-indicators span {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: background-color 0.3s;
}

.carousel-indicators span.active {
  background-color: white;
}

.carousel-indicators span:hover {
  background-color: rgba(255, 255, 255, 0.8);
}
</style>
